package com.huluobo.a2206parkdemo.news

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.RadioButton
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2206parkdemo.R
import com.huluobo.a2206parkdemo.adapter.NewsAdapter
import com.huluobo.a2206parkdemo.adapter.NewsPagingAdapter
import com.huluobo.a2206parkdemo.databinding.ActivityNewsBinding
import com.huluobo.base.bean.NewsBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.parse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsViewModel>() {
    //    private lateinit var newsAdapter: NewsAdapter
    private lateinit var newsPagingAdapter: NewsPagingAdapter
    private var page: Int = 0
    override fun initData() {
//        val map = mutableMapOf<String, Int>()
//        map[Const.PARAM_PAGE] = page
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
//        vm.getAllNews(body)
//        vm.getAllNewsSuccess.observe(this) {
//            val list = Gson().fromJson(it, NewsBean::class.java)
//            newsAdapter.data.clear()
//            newsAdapter.data.addAll(list)
//            newsAdapter.notifyDataSetChanged()
//        }

        vm.addNewsSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            binding.newsPageNumTv.text = "第${page + 1}页"
//            vm.getAllNews(body)
        }
    }

    override fun initView() {
        binding.newsHeader.headerTitle.text = "新闻"
        binding.newsHeader.headerBack.setOnClickListener { finish() }
        binding.newsHeader.headerAdd.visibility = View.VISIBLE
        binding.newsHeader.headerAdd.setOnClickListener {
            //添加新闻弹窗
            val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_news, null)
            AlertDialog.Builder(this)
                .setView(dialogView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        val newsTitle = dialogView.findViewById<EditText>(R.id.add_news_title_et).text.toString()
                        val newsValue = dialogView.findViewById<EditText>(R.id.add_news_value_et).text.toString()
                        val newsTime = getTime()
                        var tagId = -1
                        when {
                            dialogView.findViewById<RadioButton>(R.id.add_news_wuye).isChecked -> tagId = 0
                            dialogView.findViewById<RadioButton>(R.id.add_news_fun).isChecked -> tagId = 1
                            dialogView.findViewById<RadioButton>(R.id.add_news_sport).isChecked -> tagId = 2
                            dialogView.findViewById<RadioButton>(R.id.add_news_society).isChecked -> tagId = 3
                            dialogView.findViewById<RadioButton>(R.id.add_news_morning).isChecked -> tagId = 4
                        }
                        if (newsTitle.isNotEmpty() && newsValue.isNotEmpty() && newsTime.isNotEmpty() && tagId != -1) {
                            val map = mutableMapOf<String, Any>()
                            map[Const.PARAM_NEWS_TITLE] = newsTitle
                            map[Const.PARAM_NEWS_VALUE] = newsValue
                            map[Const.PARAM_NEWS_TIME] = newsTime
                            map[Const.PARAM_NEWS_TAG] = tagId

                            val json = Gson().toJson(map)
                            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                            vm.addNews(body)
                        } else {
                            ToastUtils.showLong("新闻标题或者内容不能为空")
                        }
                    }
                }).show()
        }

        binding.newsRv.layoutManager = LinearLayoutManager(this)
        newsPagingAdapter = NewsPagingAdapter()
        binding.newsRv.adapter = newsPagingAdapter

        lifecycleScope.launch {
            vm.getNewsPagingData().collect {
                newsPagingAdapter.submitData(it)
            }
        }

        binding.newsPageNumTv.text = "第${page + 1}页"

        binding.preBtn.setOnClickListener {
            if (page == 0) {
                ToastUtils.showLong("已经是第一页了")
            } else {
                page--
                binding.newsPageNumTv.text = "第${page + 1}页"
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_PAGE] = page
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.getAllNews(body)
            }
        }

        binding.nextBtn.setOnClickListener {
            page++
            binding.newsPageNumTv.text = "第${page + 1}页"
            val map = mutableMapOf<String, Int>()
            map[Const.PARAM_PAGE] = page
            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            vm.getAllNews(body)
        }


    }
}